package com.gin.kotlin.no_gay_alliance.activity.replyhistory

import android.content.Context
import android.content.Intent
import android.view.Menu
import android.view.MenuInflater
import android.view.MenuItem
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.gin.kotlin.no_gay_alliance.R
import com.gin.kotlin.no_gay_alliance.base.AbstractBindingActivity
import com.gin.kotlin.no_gay_alliance.base.FragmentStateContainer
import com.gin.kotlin.no_gay_alliance.databinding.ActivityReplyHistoryConfigBinding
import com.gin.kotlin.no_gay_alliance.fragment.replyhistory.AbstractReplyHistoryConfigFragment
import com.gin.kotlin.no_gay_alliance.fragment.replyhistory.ReplyHistoryConfigTaskFragment
import com.gin.kotlin.no_gay_alliance.fragment.replyhistory.ReplyHistoryConfigTopicFragment
import com.gin.kotlin.no_gay_alliance.fragment.replyhistory.ReplyHistoryConfigUserFragment
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator

class ReplyHistoryConfigActivity : AbstractBindingActivity<ActivityReplyHistoryConfigBinding>(), TabLayoutMediator.TabConfigurationStrategy {
    private lateinit var tabLayoutMediator: TabLayoutMediator
    lateinit var fragmentStateContainers: List<FragmentStateContainer>
    var currentFragment: AbstractReplyHistoryConfigFragment? = null

    override fun onConfigureTab(tab: TabLayout.Tab, position: Int) {
        tab.text = fragmentStateContainers[position].title
        fragmentStateContainers[position].icon?.also { tab.setIcon(it) }
    }

    override fun onResume() {
        super.onResume()
        setToolbarTitle("回复历史 - 配置")
    }

    override fun onCreateOptionsMenu(menu: Menu, menuInflater: MenuInflater) {
        super.onCreateOptionsMenu(menu, menuInflater)
        menuInflater.inflate(R.menu.add, menu)
        menuInflater.inflate(R.menu.info, menu)
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        println(currentFragment?.javaClass?.simpleName)
        return currentFragment?.onMenuItemSelected(item) ?: super.onOptionsItemSelected(item)
    }

    override fun onCreate() {
        this.fragmentStateContainers = listOf(
            FragmentStateContainer(resources.getText(R.string.tv_header_reply_history_task)) { ReplyHistoryConfigTaskFragment() },
            FragmentStateContainer(resources.getText(R.string.tv_header_reply_history_user)) { ReplyHistoryConfigUserFragment() },
            FragmentStateContainer(resources.getText(R.string.tv_header_reply_history_topic)) { ReplyHistoryConfigTopicFragment() },
        )
        val b = this.b ?: return
        // 为ViewPager2设置适配器
        b.viewPager2.adapter = object : FragmentStateAdapter(this) {
            override fun getItemCount() = fragmentStateContainers.size
            override fun createFragment(position: Int) = FragmentStateContainer.newInstance(fragmentStateContainers, position)
        }
        this.tabLayoutMediator = attachTabLayout(b.tabLayout, b.viewPager2, this)
    }

    override fun handleIntent(intent: Intent) {
    }

    companion object {
        @JvmStatic
        fun start(context: Context) {
            val starter = Intent(context, ReplyHistoryConfigActivity::class.java)
            context.startActivity(starter)
        }
    }
}